package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sl.au.entity.Option;
import com.sl.au.entity.User;
import com.sl.au.entity.EventTask;
import com.sl.au.entity.EventRegister;
import com.sl.au.entity.EventProcessToEvaluateUser;
import com.sl.au.repository.EventProcessToEvaluateUserRepository;
import com.sl.au.repository.EventTaskRepository;
import com.sl.au.service.EventProcessToEvaluateUserService;
import com.sl.au.service.OptionService;
import com.sl.au.service.EventRegisterService;
import com.sl.au.service.UserService;
@Service("eventProcessToEvaluateUserService")
@Transactional
public class EventProcessToEvaluateUserServiceImpl extends BaseServiceImpl implements EventProcessToEvaluateUserService{
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private OptionService optionService;
	@Autowired
	private EventProcessToEvaluateUserRepository eventProcessToEvaluateUserRepository;
	@Autowired
	private EventRegisterService eventRegisterService;
	@Autowired
	private UserService userService;
	@Autowired
	private EventTaskRepository eventTaskRepository;
	@Override
	public EventProcessToEvaluateUserRepository getRepository() {
		return this.eventProcessToEvaluateUserRepository;
	}
	/**
	 * 未扣分的接收超时事件处理
	 */
	@Override
	public void GetAcceptLimitProcessCountWithoutMark(){
		try {
			Option option = this.optionService.getRepository().findByTitleAndGroup("已分配", "内部事件流转状态");
			Option optionScale = this.optionService.getRepository().findByTitleAndGroup("已分配", "内部事件流转超时扣分");
			if (option != null) {
				String sql = "select a.eventId,a.taskId from (select eventId,diffTime,id as taskId from viewfortimeouttask where eventTaskState='"
						+ option.getId() + "') as a"
						+ " left join sl_wcsi_eventregister on sl_wcsi_eventregister.id= a.eventId "
						+ " left join (select registerEvent from sl_wcsi_eventprocesstoevaluateuser where processType='"
						+ option.getId() + "') as b on b.registerEvent=a.eventId "
						+ " where b.registerEvent is NULL and a.diffTime-sl_wcsi_eventregister.acceptLimitSpan>0";
				List<Map<String, Object>> arrys = this.jdbcTemplate.queryForList(sql);

				List<EventProcessToEvaluateUser> dlist = new ArrayList<>();

				for (Map<String, Object> arry : arrys) {
					String eventId = arry.get("eventId").toString();
					EventRegister eventRegister = this.eventRegisterService.getRepository().findOne(eventId);
					List<User> users = this.userService.getRepository().findUsersByEventTypeAndGrid(
							eventRegister.getRegisterEventType().getId(),
							eventRegister.getRegisterEventToNetGrid().getId());
					String taskId = arry.get("taskId").toString();
					EventTask task= this.eventTaskRepository.findOne(taskId);
					for (int i = 0; i < users.size(); i++) {
						EventProcessToEvaluateUser eventProcessToEvaluateUser = new EventProcessToEvaluateUser();
						eventProcessToEvaluateUser.setCreateTime(DateTime.now().toDate());
						eventProcessToEvaluateUser.setAccTime(task.getEventTaskTime());
						eventProcessToEvaluateUser.setProcessType(option);
						eventProcessToEvaluateUser.setRegisterEvent(eventRegister);
						eventProcessToEvaluateUser.setRegisterUser(users.get(i));
						eventProcessToEvaluateUser.setScale(Double.parseDouble(optionScale.getValue()));
						eventProcessToEvaluateUser.setTaskId(taskId);
						eventProcessToEvaluateUser.setUpdateTime(DateTime.now().toDate());
						dlist.add(eventProcessToEvaluateUser);
					}
				}
				if(dlist.size()>0){
					this.eventProcessToEvaluateUserRepository.save(dlist);
				}
			}
		} catch (Exception ex) {
		}
	}
	/**
	 * 未扣分的处理超时事件处理
	 */
	@Override
	public void GetDealLimitProcessCountWithoutMark(){
		try {
			Option option = this.optionService.getRepository().findByTitleAndGroup("处理中", "内部事件流转状态");
			Option optionScale = this.optionService.getRepository().findByTitleAndGroup("处理中", "内部事件流转超时扣分");
			if (option != null) {
				String sql = "select a.eventId,a.taskId,a.eventTaskUser from (select eventId,diffTime,id as taskId,eventTaskUser from viewfortimeouttask where eventTaskState='"
						+ option.getId() + "') as a"
						+ " left join sl_wcsi_eventregister on sl_wcsi_eventregister.id= a.eventId "
						+ " left join (select registerEvent from sl_wcsi_eventprocesstoevaluateuser where processType='"
						+ option.getId() + "') as b on b.registerEvent=a.eventId "
						+ " where b.registerEvent is NULL and a.diffTime-sl_wcsi_eventregister.dealLimitSpan>0";
				List<Map<String, Object>> arrys = this.jdbcTemplate.queryForList(sql);

				List<EventProcessToEvaluateUser> dlist = new ArrayList<>();

				for (Map<String, Object> arry : arrys) {
					String eventId = arry.get("eventId").toString();
					EventRegister eventRegister = this.eventRegisterService.getRepository().findOne(eventId);

					String taskId = arry.get("taskId").toString();
					EventTask task= this.eventTaskRepository.findOne(taskId);
					User user = this.userService.getRepository().getOne(arry.get("eventTaskUser").toString());
					EventProcessToEvaluateUser eventProcessToEvaluateUser = new EventProcessToEvaluateUser();
					eventProcessToEvaluateUser.setCreateTime(DateTime.now().toDate());
					eventProcessToEvaluateUser.setAccTime(task.getEventTaskTime());
					eventProcessToEvaluateUser.setProcessType(option);
					eventProcessToEvaluateUser.setRegisterEvent(eventRegister);
					eventProcessToEvaluateUser.setRegisterUser(user);
					eventProcessToEvaluateUser.setScale(Double.parseDouble(optionScale.getValue()));
					eventProcessToEvaluateUser.setTaskId(taskId);
					eventProcessToEvaluateUser.setUpdateTime(DateTime.now().toDate());
					dlist.add(eventProcessToEvaluateUser);
				}
				if (dlist.size() > 0) {
					this.eventProcessToEvaluateUserRepository.save(dlist);
				}
			}
		} catch (Exception ex) {
		}
	}
}
